#ifndef _LIGHT_H
#define _LIGHT_H

#include <Arduino.h>
#include <XOutput.h>
#include <Servo.h>
#include <EEPROM.h>

class Light {
public:
  Light(int seLeftPin, int seRightPin, int seEnablePin, bool seEnableLevel, uint8_t validFlag, void (*delay)(unsigned long) = delay):
    _seLeftPin(seLeftPin), _seRightPin(seRightPin), enable(seEnablePin, seEnableLevel), _validFlag(validFlag), _delay(delay) { }
  struct {
    char validFlag;
    int address = 3000;
    int leftOn, leftOff, leftDefault, rightOn, rightOff, rightDefault;
    void init(uint8_t checkFlag)
    {
      validFlag = checkFlag;
      leftOn = 90;
      leftOff = 90;
      leftDefault = 90;
      rightOn = 90;
      rightOff = 90;
      rightDefault = 90;
    }
    bool write(void)
    {
      for(size_t i = 0; i < sizeof(*this); i++)
      {
        EEPROM.write(address + i, ((uint8_t *)this)[i]);
      }
      return EEPROM.commit();
    }
    void read(void)
    {
      for(size_t i = 0; i < sizeof(*this); i++)
      {
        ((uint8_t *)this)[i] = EEPROM.read(address + i);
      }
    }
    bool checkValid(uint8_t checkFlag)
    {
      return validFlag == checkFlag;
    }
  } data;
  Light &init(void);
  Light &start(void);
  Light &finish(void);
  Light &leftOn(unsigned long nms = 0);
  Light &leftOff(unsigned long nms = 0);
  Light &leftReset(unsigned long nms = 0);
  Light &rightOn(unsigned long nms = 0);
  Light &rightOff(unsigned long nms = 0);
  Light &rightReset(unsigned long nms = 0);
  Light &on(unsigned long nms = 0);
  Light &off(unsigned long nms = 0);
  Light &reset(unsigned long nms = 0);
  Light &dataReset(void);
private:
  int _seLeftPin, _seRightPin;
  void (*_delay)(unsigned long);
  uint8_t _validFlag;
  Servo left, right;
  XOutput enable;
};

#endif // _LIGHT_H
